<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Delivery;

use App\Func\UploadFile;
use App\Models\Delivery\DeliveryReview;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class DeliveryReviewAjax {

    /**
     * 删除标签，并修改tag_status=1，tag_name=null
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteTag($id){
        $reviewObj = DeliveryReview::query()->find($id);
        if($reviewObj->status == 2){
            return response()->json(['status' => false, 'message' => '已确认勿删除！']);
        }
        $uploadFile = UploadFile::getInit();
        $fileDir = DeliveryReview::fileDir($reviewObj->type, strtotime($reviewObj->created_at));
        if(!$uploadFile->delFile($fileDir.DIRECTORY_SEPARATOR.$reviewObj->tag_name)){
            return response()->json(['status' => false, 'message' => '操作失败！']);
        }else{
            $reviewObj->tag_status = 1;
            $reviewObj->tag_name = null;
            if(!$reviewObj->save()){
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }else{
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }
        }
    }

    /**
     * 确认
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirm($id){
        $reviewObj = DeliveryReview::query()->find($id);
        $reviewObj->status = 2;
        if(!$reviewObj->save()){
            return response()->json(['status' => false, 'message' => '操作失败！']);
        }else{
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }
    }

    /**
     * 验证标签
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkModal($id){
        $reviewObj = DeliveryReview::query()->find($id);
        if($reviewObj->tag_status == 2){
            return response()->json(['status' => false, 'message' => '请刷新后重试！']);
        }else{
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }
    }

    public function tagFile($id){
        return view('delivery.delivery-review.modal-tag',compact('id'));
    }

    /**
     * 上传标签
     * @param UploadedFile $file
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function doUploadTag(UploadedFile $file, $param){
        if($file->isValid()){
            try{
                DB::beginTransaction();
                $reviewObj = DeliveryReview::query()->findOrFail($param['id']);
                $fileDir = DeliveryReview::fileDir($reviewObj->type, strtotime($reviewObj->created_at));
                $uploadFile = UploadFile::getInit();
                $tagName = $reviewObj->id.'_+'.$file->getClientOriginalName();
                if(!$uploadFile->upload($file, $fileDir, false,$tagName)){
                    return response()->json(['status' => false, 'message' => '操作失败！']);
                }else{
                    $reviewObj->tag_name = $tagName;
                    $reviewObj->tag_status = 2;
                    if(!$reviewObj->save()){
                        DB::rollBack();
                        return response()->json(['status' => false, 'message' => '操作失败！']);
                    }else{
                        DB::commit();
                        return response()->json(['status' => true, 'message' => '操作成功！']);
                    }
                }
            }catch (\Exception $e){
                DB::rollBack();
                report($e);
                return response()->json(['status' => false, 'message' => '系统异常！']);
            }
        }else{
            return response()->json(['status' => false, 'message' => '无效文件！']);
        }
    }
}
